In many situations, you need to check the result of
an operation to tell if it succeeded. The result-checking functions in
Visual Basic let you test results before you take actions that might
otherwise cause an error. Table 1 lists the result-checking functions.
Table 1. Visual Basic result-checking functions
Category | Function | Use to |
---|
Boolean tests | IsArray | Tell if a variable is an array |
| IsDate | Tell if a variable contains data that can be converted to a Date |
| IsEmpty | Tell if a variable has not yet been initialized |
| IsError | Tell if a variable contains an Error object |
| IsMissing | Tell if a ParamArray argument was omitted |
| IsNull | Tell if a variable contains no valid data |
| IsNumeric | Tell if a variable contains a value that can be converted to a number |
| IsObject | Tell if a variable is a reference to a valid object |
Type tests | TypeName | Get the name of the variable's type as a string |
| TypeOf | Test the type of a variable within an If block |
| VarType | Get the variable's type as a VbVarType constant |
Most of these tests are used with variables that were declared as Variant or Object
data types. Those types of variables can contain many different kinds
of data, so it is often necessary to test what the variable contains
before proceeding in code.
There are several common uses of this in Excel. The first is ActiveSheet.property, which may refer to a Worksheet, Chart, or other object:
Sub ChangeSheets( )
Select Case TypeName(ActiveSheet)
Case "Worksheet"
If ActiveSheet.Index < Worksheets.Count Then
Worksheets(ActiveSheet.Index + 1).Activate
Else
Worksheets(1).Activate
End If
Case "Chart"
If ActiveSheet.Index < Charts.Count Then
Charts(ActiveSheet.Index + 1).Activate
Else
Charts(1).Activate
End If
Case Else
Debug.Print TypeName(ActiveSheet), ActiveSheet.Name
End Select
End Sub
The preceding code uses a Select statement to perform different actions based on the TypeName of the active sheet. You usually combine TypeName with SelectTypeName is also handy for checking whether or not an optional argument has been omitted: when there are more than two possibilities as shown in the preceding block.
Public Sub Reformat(Optional ws As Worksheet)
' Check if argument was omitted.
If TypeName(ws) = "Nothing" Then
' Check the type of the active sheet.
If TypeName(ActiveSheet) = "Worksheet" Then
' Format the active worksheet.
Set ws = ActiveSheet
Else
' You can't reformat nonworksheets.
MsgBox "Select a worksheet and try again."
Exit Sub
End If
End If
Dim rng As Range
' Get the cells with data in them.
Set rng = ws.UsedRange
' Apply AutoFormat
rng.AutoFormat xlRangeAutoFormatSimple
End Sub
In the preceding code,
the choices are either/or: if the argument is omitted, check the active
sheet; if that sheet is a worksheet, use it. Alternately, you can use
the TypeOf keyword within an If statement; however, TypeOf can't test if the variable is Nothing. To do that, you need to use either TypeName or the Is operator, as shown by this different version of the preceding code:
Public Sub Reformat2(Optional ws As Worksheet)
' Check if argument was omitted.
If ws Is Nothing Then
' Check the type of the active sheet.
If TypeOf ActiveSheet Is Worksheet Then
' Format the active worksheet.
Set ws = ActiveSheet
Else
' You can't reformat nonworksheets.
MsgBox "Select a worksheet and try again."
Exit Sub
End If
End If
Dim rng As Range
' Get the cells with data in them.
Set rng = ws.UsedRange
' Apply AutoFormat
rng.AutoFormat xlRangeAutoFormatSimple
End Sub
Reformat and Reformat2 are equivalent. I tend to use the TypeName test rather than TypeOf or Is because it lets me use a consistent test for all types of objects, but that's really just a personal preference.
The IsNumeric and IsDate
functions are useful when receiving data from a user. Rather than
returning specific information about the type of the variable, they let
you know if the data in the variable can be converted to those types.
For instance, the following code checks the value entered in an InputBox to determine the type of entry the user made:
Sub CheckEntry( )
Dim var As String, msg As String
var = InputBox("Enter a number, word, or date.")
' Use Boolean tests to check an entry.
If IsNumeric(var) Then
msg = "number."
ElseIf IsDate(var) Then
msg = "date."
ElseIf var = "" Then
msg = "empty."
Else
msg = "string."
End If
Debug.Print "Entry is a " & msg
End Sub
IsNumeric and IsDate are a good way to check values before calling conversion functions like CDate or CDouble.